package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Order extends Activity  {
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView listView;
     private ArrayList<OrderList> mList=new ArrayList<>();

    private int  id;
    private ListAdapter adapter;
    public   int current_page;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        initData();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(Order.this,OrderDetial.class);
                intent.putExtra("id",mList.get(position).id);
                startActivity(intent);
            }
        });

        SharedPreferences sharedPreferences=getSharedPreferences("data",0);
        final String access_token= sharedPreferences.getString("access_token",null);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState){
                    case AbsListView.OnScrollListener
                            .SCROLL_STATE_IDLE:
                        if (listView.getLastVisiblePosition()==(listView.getCount()-1)){


                            new Thread(new Runnable() {
                                @Override
                                public void run() {

                                    HttpContect httpContect=new HttpContect();
                                    String route="/api/order/my-order";

                                    Date beginDate=initDate();

                                    long beginDateTime=beginDate.getTime();
                                    String created_at= String.valueOf(beginDateTime/1000);


                                    String data="access_token="+access_token+"&created_at="+created_at+"&page="+ ++current_page;
                                    Log.i("1233","-------------------------------------------------"+data);
                                    String JsonResolutioValue= httpContect.send(route,data);

                                    Log.i("123","_______________________"+JsonResolutioValue);

                                    Message msg = new Message();
                                    msg.obj = JsonResolutioValue;
                                    handler.sendMessage(msg);

                                }
                            }).start();
                        }
break;
                }

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });


        adapter = new ListAdapter(this);
        listView.setAdapter(adapter);


            new Thread(new Runnable() {
                @Override
                public void run() {


                    HttpContect httpContect=new HttpContect();
                    String route="api/order/my-order";

                    Date beginDate=initDate();

                    long beginDateTime=beginDate.getTime();
                    String created_at= String.valueOf(beginDateTime/1000);
                    Log.i("123456",created_at);

                    String data="access_token="+access_token+"&created_at="+created_at;
                    String JsonResolutioValue= httpContect.send(route,data);

Message msg = new Message();
msg.obj = JsonResolutioValue;
handler.sendMessage(msg);

                }
            }).start();




        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (swipeRefreshLayout.isRefreshing()){

                    new Thread(new Runnable() {
                        @Override
                        public void run() {

                            HttpContect httpContect=new HttpContect();
                            String route="/api/order/my-order";

                            Date beginDate=initDate();

                            long beginDateTime=beginDate.getTime();
                            String created_at= String.valueOf(beginDateTime/1000);
                            Log.i("123456",created_at);
                            String data="access_token="+access_token+"&created_at="+created_at+"&page="+1;
                            String JsonResolutioValue= httpContect.send(route,data);


                            Message msg = new Message();
                            msg.obj = JsonResolutioValue;
                            Log.i("123",JsonResolutioValue);
                            handler.sendMessage(msg);


                        }
                    }).start();



                    swipeRefreshLayout.setRefreshing(false);



                }



            }
        });

    }

    private void initData() {
        swipeRefreshLayout=findViewById(R.id.sw1);
        listView=findViewById(R.id.lv1);

    }

private int JsonResolution(String JsonResolutioValue ){

    try {
        JSONObject jsonObject=new JSONObject(JsonResolutioValue);

        int error=jsonObject.getInt("error");
        String message=jsonObject.getString("message");
        JSONObject data=jsonObject.getJSONObject("data");
        int total=data.getInt("total");
        int per_page=data.getInt("per_page");
         current_page =data.getInt("current_page");
            Log.i("4156","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+current_page);
        int last_page=data.getInt("last_page");
        JSONArray list=data.getJSONArray("list");
if (swipeRefreshLayout.isRefreshing()){
    mList.clear();
    Log.i("123123214","$$$$$$$$$$$$@!!@#!@#"+mList.size());
}



        for (int i=0;i<list.length();i++){
          OrderList  item = new OrderList();
           JSONObject jsonObject1= (JSONObject) list.get(i);
          id=jsonObject1.getInt("id");
            int created_at=jsonObject1.getInt("created_at");
            int  price= jsonObject1.getInt("price");
            int pay_type=jsonObject1.getInt("pay_type");
            String sku_name=jsonObject1.getString("sku_name");
             String store_name=jsonObject1.getString("store_name");
            int goods_image=jsonObject1.getInt("goods_image");
            item.setPrice(price);
            item.setStore_name(store_name);
            item.setSku_name(sku_name);
            item.setCreated_at(created_at);
            item.setId(id);
mList.add(item);
}

        Log.i("123123","!!!@#!!!!!!!!$$$$$$$$$$$$$$"+mList.size());
adapter.setData(mList,current_page);


            return current_page;

    } catch (JSONException e) {
        e.printStackTrace();
    }


    return 0;
}

 Handler handler = new Handler(){
     @Override
     public void handleMessage(Message msg) {

        String data = (String) msg.obj;
        Log.i("12",data);
        JsonResolution(data);

     }
 };

public Date initDate(){
    Calendar calendar=Calendar.getInstance();
    calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),0,0,0);
    Date beginDate=calendar.getTime();

        return beginDate;

}


}

